#include <iostream>
#include <cstdlib>
#include <ctime>

#define HEIGHT 10
#define WIDTH 10
#define MINE_COUNT 10

struct Square {
    bool isMine;
    int numAdjacentMines;
    bool isRevealed;
};

Square gameBoard[HEIGHT][WIDTH];

void initializeBoard() {
    srand(time(0));
    for (int i = 0; i < HEIGHT; ++i) {
        for (int j = 0; j < WIDTH; ++j) {
            gameBoard[i][j].isMine = rand() % (MINE_COUNT + 1) == 0;
            gameBoard[i][j].numAdjacentMines = 0;
            gameBoard[i][j].isRevealed = false;
        }
    }

    // 避免边缘地雷导致计数错误
    for (int i = 0; i < MINE_COUNT; ++i) {
        int mineX, mineY;
        do {
            mineX = rand() % HEIGHT;
            mineY = rand() % WIDTH;
        } while (gameBoard[mineX][mineY].isMine);
        gameBoard[mineX][mineY].isMine = true;

        // 更新相邻地雷计数
        for (int dx = -1; dx <= 1; ++dx) {
            for (int dy = -1; dy <= 1; ++dy) {
                if (mineX + dx >= 0 && mineX + dx < HEIGHT &&
                    mineY + dy >= 0 && mineY + dy < WIDTH &&
                    !gameBoard[mineX + dx][mineY + dy].isMine) {
                    gameBoard[mineX + dx][mineY + dy].numAdjacentMines++;
                }
            }
        }
    }
}

void revealSquare(int x, int y) {
    if (x < 0 || x >= HEIGHT || y < 0 || y >= WIDTH)
        return;
    if (gameBoard[x][y].isRevealed)
        return;
    gameBoard[x][y].isRevealed = true;
    if (!gameBoard[x][y].isMine) {
        if (gameBoard[x][y].numAdjacentMines == 0)
            for (int dx = -1; dx <= 1; ++dx)
                for (int dy = -1; dy <= 1; ++dy)
                    revealSquare(x + dx, y + dy);
    }
}

bool checkWin() {
    for (int i = 0; i < HEIGHT; ++i) {
        for (int j = 0; j < WIDTH; ++j) {
            if (!gameBoard[i][j].isRevealed && !gameBoard[i][j].isMine)
                return false;
        }
    }
    return true;
}

int main() {
    initializeBoard();
    int x, y;
    while (true) {
        std::cout << "请输入坐标（例如 0 0）：" << std::endl;
        std::cin >> x >> y;
        revealSquare(x, y);
        if (checkWin()) {
            std::cout << "恭喜！你赢了！" << std::endl;
            break;
        } else if (gameBoard[x][y].isMine) {
            std::cout << "很遗憾，你踩到地雷了！" << std::endl;
            break;
        }
    }
    return 0;
}